#!/bin/sh
# This regression test is a part of SIPp.
# The calls -reconnect_close ends with a TCP connection take what they
# left waiting to be written with them: the INVITE of a call that had
# failed went on the next connection, and the peer answered a dead call.
. "`dirname "$0"`/../functions"; init

# The UAC's first connection is refused before any call. Its second,
# two seconds later, is refused with call 1's INVITE waiting, which ends
# call 1. The UAS listens by the third, which carries call 2. Both calls
# are due by the second, and the reset would end both: -l 1 holds call 2
# back until call 1 has ended.
rm -f uas.log
(sleep 3; sipp -nostdin -sn uas -t t1 -i 127.0.0.1 -p 27875 -timeout 8 \
    -trace_msg -message_file uas.log >/dev/null 2>&1) &
uas=$!
sippfg -sn uac -t t1 -i 127.0.0.1 -p 27874 127.0.0.1:27875 -m 2 -l 1 \
    -max_reconnect 3 -reconnect_sleep 2000 -timeout 10 >/dev/null 2>&1
kill $uas 2>/dev/null
wait $uas 2>/dev/null
calls=`sed -n 's/^Call-ID: \([0-9]*\)-.*/\1/p' uas.log 2>/dev/null | sort -u`
rm -f uas.log

if echo "$calls" | grep -qx 1; then
    fail "the UAS got the INVITE of the failed call 1"
elif ! echo "$calls" | grep -qx 2; then
    fail "the UAS did not get call 2"
else
    ok
fi
